package org.example.test_03.Service.Impl;

import org.example.test_03.Domian.ReturnData;
import org.example.test_03.Domian.Student;
import org.example.test_03.Mapper.StudentMapper;
import org.example.test_03.Service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author lands
 * @Date 2024/8/6 10:23
 * @Description TODO
 */
@Service
public class StudentServiceImpl  implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public ReturnData selects(String username) {
        return ReturnData.ok(studentMapper.selectUser(username));
    }

    @Override
    public ReturnData getStuById(int id) {
        return ReturnData.ok(studentMapper.getStuById(id));
    }

    @Override
    public ReturnData delete(Integer id) {
        int result = studentMapper.delete(id);
        if (result == 0) {
            return ReturnData.error("删除失败");
        } else {
            return ReturnData.ok("删除成功");
        }

    }

    @Override
    public ReturnData edit(Student student) {
        //注意，一定要校验数据
        if(student.getUsername().equals("")){
            return ReturnData.error("用户名不能为空");
        }
        if(student.getId()==0){//新增
            int result = studentMapper.add(student.getUsername(), student.getPassword(), student.getAge(), student.getEmail());
            if (result == 0) {
                return ReturnData.error("新增失败");
            } else {
                return ReturnData.ok("新增成功");
            }

        }else{//编辑
            int result = studentMapper.edit(student.getId(), student.getUsername(), student.getPassword(), student.getAge(), student.getEmail());
            if (result == 0) {
                return ReturnData.error("编辑失败");
            } else {
                return ReturnData.ok("编辑成功");
            }
        }

    }
}
